<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 什么是偏函数？偏函数就是将一个 n 参的函数转换成固定 x 参的函数，剩余参数（n - x）将在下次调用全部传入。举个例子：
        // example
        function add(x, y, z) {
            return x + y + z
        }
        var partialAdd = partial(add, 1);
        partialAdd(2, 3)

        // 简单版 偏函数
        function partial(fn, ...args) {
            return function (...args2) {
                fn.call(this, ...args, ...args2)
            }
        }

        // 实现占位功能
        function clg(a, b, c, d) {
            console.log(a, b, c, d)
        }
        var partialClg = partial1(clg, '_', 2, '_');
        partialClg(1, 3, 4)
        function partial1(fn, ...args) {
            var placeholderNum = 0;
            return function (...args2) {
                args2.forEach(arg => {
                    var index = args.findIndex(item => item == "_");
                    args[index] = arg;
                    placeholderNum++
                })
                if (placeholderNum < args2.length) {
                    args2 = args2.slice(placeholderNum)
                }
                return fn.call(this, ...args, ...args2)
            }

        }
    </script>
</body>

</html>